package com.kcs.dc.report.model.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import com.kcs.common.utils.Constant;
import com.kcs.common.utils.LogWriter;
import com.kcs.dc.report.model.dao.model.headermenuModel;


public class headermenuDAO {
	
	public  List<headermenuModel> getListMenuOfLoginUser(String user_id, Connection conn) throws Exception {
		
		CallableStatement proc = null;
		ResultSet rs = null;
		headermenuModel model = null;
		ArrayList<headermenuModel> result = new ArrayList<headermenuModel>(); 
		
		try {
			proc = conn.prepareCall("{ call qry_user_authorized_proc(?, ?, ?, ?) }");
			
			proc.setString(1, user_id);
			proc.setString(2, "");   // user_name is blank
			proc.setString(3, "0");  // menu_name is 0 for list off all menu
			proc.registerOutParameter(4, Types.INTEGER);
			
			rs = proc.executeQuery();
			while (rs.next()) {
				model = new headermenuModel();
				model.setUser_id(rs.getString("user_id"));
				model.setMenu_level(rs.getInt("menu_level"));
				model.setMenu_shortcode(rs.getString("menu_shtcode"));
				model.setMenu_name(rs.getString("menu_name"));
				model.setMenu_parent(rs.getString("menu_parent"));
				model.setMenu_url(rs.getString("menu_url"));
				model.setMenu_action(rs.getString("menu_action"));
				model.setActive_status(rs.getString("active_status"));
				result.add(model);			
			}
				
		} catch (Exception e) {
			LogWriter.error("1.1 headermenuDAO [Method : getListMenuOfLoginUser] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
			throw new Exception();
		} finally {
			if (proc != null) {
				try {
					proc.close();
				} catch (SQLException e) {
					LogWriter.error("1.2 headermenuDAO [Method : getListMenuOfLoginUser] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new Exception();
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					LogWriter.error("1.3 headermenuDAO [Method : getListMenuOfLoginUser] : " + e.getMessage(), Constant.LOGGER_APPLICATION);
					throw new Exception();
				}
			}
		}
		
		return result;
		
	}
}
